//
//  ColumeBarController.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/6.
//  Copyright © 2016年 吴可高. All rights reserved.

@interface ColumeDrawView : UIView
{
    CGRect _columnDrawViewFrame;
    
}
@end

@implementation ColumeDrawView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

- (void)initSetUp
{
    CGRect rect = self.frame;
    UIBezierPath * bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(CGRectGetMinX(rect)+1, CGRectGetMinY(rect)+1, CGRectGetWidth(rect)-2, CGRectGetHeight(rect)-2) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
    //
    CAShapeLayer * radiiShapeLayer = [CAShapeLayer layer];
    radiiShapeLayer.path = bezierPath.CGPath;
    radiiShapeLayer.fillColor = KColor(247, 247, 247).CGColor;
    radiiShapeLayer.strokeColor = KColor(0, 0, 0).CGColor;
    [self.layer addSublayer:radiiShapeLayer];
    
    // CoreAnimation
    CABasicAnimation  * radiiBaseAnimation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
    radiiBaseAnimation.fromValue = @(0);
    radiiBaseAnimation.toValue = @(1);
    radiiBaseAnimation.duration = 3.f;
    radiiBaseAnimation.removedOnCompletion = NO;
    radiiBaseAnimation.fillMode = kCAFillModeForwards;
    radiiBaseAnimation.autoreverses = NO;
    [radiiShapeLayer addAnimation:radiiBaseAnimation forKey:NSStringFromSelector(@selector(strokeEnd))];
}
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    // 绘制贝塞尔曲线path ｜
    CGPoint  startDrewPoint = CGPointMake(10, kNavigationBarHeight*1.1);
    /*
     CGContextRef conetextRef = UIGraphicsGetCurrentContext();
     CGContextSetStrokeColorWithColor(conetextRef, [UIColor blueColor].CGColor);
     CGContextSetLineWidth(conetextRef, 2.f);
     CGContextMoveToPoint(conetextRef, startDrewPoint.x,startDrewPoint.y);
     CGContextAddLineToPoint(conetextRef,kScreen_Width -20, kNavigationBarHeight *1.5);
     CGContextAddLineToPoint(conetextRef, (kScreen_Width-20)/2.f, kNavigationBarHeight*2.f);
     
     CGContextAddQuadCurveToPoint(conetextRef, (kScreen_Width-20)/2.f, kNavigationBarHeight*2.2f, (kScreen_Width-20)/4.f, kNavigationBarHeight * 2.5f);
     
     CGContextAddArc(conetextRef, self.center.x, self.center.y, (kScreen_Width-20)/8.f, 0, 4/3.f * M_PI, NO);
     CGContextStrokePath(conetextRef);
     
     UIBezierPath * animationBezierPath = [UIBezierPath bezierPath];
     [animationBezierPath moveToPoint:startDrewPoint];
     [animationBezierPath addLineToPoint:CGPointMake(kScreen_Width -20, kNavigationBarHeight *1.5)];
     [animationBezierPath addLineToPoint:CGPointMake((kScreen_Width-20)/2.f, kNavigationBarHeight *2.f)];
     //
     [animationBezierPath addQuadCurveToPoint:CGPointMake((kScreen_Width-20)/2.f, kNavigationBarHeight *2.f) controlPoint:CGPointMake((kScreen_Width-20)/4.f, kNavigationBarHeight * 4.5f)];
     [animationBezierPath addArcWithCenter:CGPointMake(self.center.x, self.center.y) radius:(kScreen_Width-20)/8.f startAngle:0 endAngle:4/3.0 * M_PI clockwise:YES];
     */
    CGMutablePathRef  mutableRef = CGPathCreateMutable();
    CGPathMoveToPoint(mutableRef, &CGAffineTransformIdentity, startDrewPoint.x, startDrewPoint.y);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity,kScreen_Width -20, kNavigationBarHeight *1.5);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity,(kScreen_Width-20)/2.f, kNavigationBarHeight*2.f);
    CGPathAddQuadCurveToPoint(mutableRef, &CGAffineTransformIdentity, (kScreen_Width-20)/2.f, kNavigationBarHeight *2.f,(kScreen_Width-20)/4.f, kNavigationBarHeight * 4.5f);
    CGPathAddArc(mutableRef, &CGAffineTransformIdentity,self.center.x, self.center.y, (kScreen_Width-20)/8.f, 0, 4/3.f *M_PI, NO);
    
    
    CAShapeLayer * shapeAnimationLayer = [CAShapeLayer layer];
    shapeAnimationLayer.strokeColor = [UIColor blueColor].CGColor;
    shapeAnimationLayer.fillColor  =  [UIColor clearColor].CGColor;
    shapeAnimationLayer.lineWidth = 2.f;
    shapeAnimationLayer.path = mutableRef;
    [self.layer addSublayer:shapeAnimationLayer];
    
    CABasicAnimation * shapeAnimation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
    shapeAnimation.duration = 3.f;
    shapeAnimation.fromValue =@(0);
    shapeAnimation.toValue  =@(1);
    shapeAnimation.autoreverses = NO;
    shapeAnimation.removedOnCompletion = NO;
    shapeAnimation.fillMode =kCAFillModeForwards;
    shapeAnimation.delegate = self;
    [shapeAnimationLayer addAnimation:shapeAnimation forKey:NSStringFromSelector(@selector(strokeEnd))];
    [self drawIndicatorOrigin:startDrewPoint mutablePath:mutableRef];
}
-(void)drawIndicatorOrigin:(CGPoint)origin mutablePath:(CGPathRef)mutablePathRef
{
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.backgroundColor = [UIColor redColor].CGColor;
    shapeLayer.cornerRadius = 10.f/2.f;
    shapeLayer.masksToBounds = YES;
    shapeLayer.frame = CGRectMake(0, 0, 10, 10);
    shapeLayer.position  = CGPointMake(origin.x, origin.y);
    [self.layer addSublayer:shapeLayer];
    
    CAKeyframeAnimation * keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.delegate = self;
    keyAnimation.duration = 3.f;
    keyAnimation.autoreverses = NO;
    keyAnimation.fillMode = kCAFillModeForwards;
    keyAnimation.removedOnCompletion  = NO;
    
    keyAnimation.path = mutablePathRef;
    keyAnimation.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [shapeLayer addAnimation:keyAnimation forKey:@"position"];
    
}
-(void)animationDidStart:(CAAnimation *)anim
{
    [[UIApplication sharedApplication]beginIgnoringInteractionEvents];
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
@end


@interface GradientSpaceView : UIView
@end
@implementation GradientSpaceView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp
{
    self.backgroundColor = [UIColor lightGrayColor];
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef  contextRef =  UIGraphicsGetCurrentContext();
    CGPoint  startPoint = CGPointMake(10, kNavigationBarHeight*2);
    /*
     CGContextSetStrokeColorWithColor(contextRef, [UIColor blueColor].CGColor);
     CGContextSetLineWidth(contextRef, 2.f);
     CGContextMoveToPoint(contextRef, startPoint.x, startPoint.y);
     CGContextAddLineToPoint(contextRef,kScreen_Width*.2, kNavigationBarHeight*2 + 10);
     CGContextAddLineToPoint(contextRef,kScreen_Width*.4, kNavigationBarHeight*2 -15);
     CGContextAddLineToPoint(contextRef,kScreen_Width*.6, kNavigationBarHeight*2 +20);
     CGContextAddLineToPoint(contextRef,kScreen_Width *.8, kNavigationBarHeight*2 +5);
     CGContextAddLineToPoint(contextRef,kScreen_Width*.9, kNavigationBarHeight*2 -5);
     CGContextAddLineToPoint(contextRef,kScreen_Width-10, kNavigationBarHeight*2);
     CGContextStrokePath(contextRef);
     CGContextDrawPath(contextRef, kCGPathStroke);
     
     CGContextSaveGState(contextRef);
     CGContextSetStrokeColorWithColor(contextRef, [UIColor redColor].CGColor);
     CGContextSetLineWidth(contextRef, 4.f);
     CGContextMoveToPoint(contextRef,kScreen_Width-10, kNavigationBarHeight*2);
     CGContextAddLineToPoint(contextRef,kScreen_Width-10, CGRectGetHeight(rect)-10);
     CGContextAddLineToPoint(contextRef,10, CGRectGetHeight(rect)-10);
     CGContextAddLineToPoint(contextRef,startPoint.x,startPoint.y);
     CGContextDrawPath(contextRef, kCGPathStroke);
     CGContextRestoreGState(contextRef);
     */
    /*
    CGContextSetStrokeColorWithColor(contextRef, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(contextRef, 2.f);
    CGMutablePathRef mutableRef  = CGPathCreateMutable();
    CGPathMoveToPoint(mutableRef, &CGAffineTransformIdentity, startPoint.x, startPoint.y);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width*.2, kNavigationBarHeight*2 + 10);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width*.4, kNavigationBarHeight*2 -15);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width*.6, kNavigationBarHeight*2 +20);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width *.8, kNavigationBarHeight*2 +5);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width*.9, kNavigationBarHeight*2 -5);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width-10, kNavigationBarHeight*2);
    CGContextAddPath(contextRef, mutableRef);
    CGContextStrokePath(contextRef);
    CGContextDrawPath(contextRef, kCGPathStroke);
    
    CGPathMoveToPoint(mutableRef, &CGAffineTransformIdentity,kScreen_Width-10, kNavigationBarHeight*2);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, kScreen_Width-10, CGRectGetHeight(rect)-10);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, 10, CGRectGetHeight(rect)-10);
    CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity,startPoint.x,startPoint.y);
    CGContextAddPath(contextRef, mutableRef);
    CGContextStrokePath(contextRef);
    CGContextDrawPath(contextRef, kCGPathStroke);
    */
    
    UIBezierPath * bezierPath  = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:startPoint];
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width*.2, kNavigationBarHeight*2 + 10)];
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width*.4, kNavigationBarHeight*2 -15)];
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width*.6, kNavigationBarHeight*2 +20)];
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width *.8, kNavigationBarHeight*2 +5)];
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width*.9, kNavigationBarHeight*2 -5)];
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width-10, kNavigationBarHeight*2)];
    
    
    [bezierPath addLineToPoint:CGPointMake(kScreen_Width-10, CGRectGetHeight(rect)-10)];
    [bezierPath addLineToPoint:CGPointMake(10, CGRectGetHeight(rect)-10)];
    [bezierPath addLineToPoint:startPoint];
    //
    CAShapeLayer * gradientShapeLayer = [CAShapeLayer layer];
    gradientShapeLayer.lineCap = kCALineCapRound;
    gradientShapeLayer.lineJoin = kCALineJoinRound;
    gradientShapeLayer.lineWidth = 2.f;
    gradientShapeLayer.strokeColor  = [UIColor blueColor].CGColor;
    gradientShapeLayer.fillColor =  [UIColor clearColor].CGColor;
    gradientShapeLayer.path  =  bezierPath.CGPath;
    [self.layer addSublayer:gradientShapeLayer];
    [self drawGradientLayerWithContextRef:contextRef path:bezierPath.CGPath startColor:[UIColor colorWithRed:255.0/255.0 green:135.0/255.0 blue:51.0/255.0 alpha:0.1].CGColor endColor:[UIColor whiteColor].CGColor];
    
    
    CABasicAnimation * gradientAnimation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
    gradientAnimation.duration = 2.f;
    gradientAnimation.fromValue  = @(0);
    gradientAnimation.toValue    = @(1);
    gradientAnimation.autoreverses = NO;
    gradientAnimation.removedOnCompletion = NO;
    gradientAnimation.fillMode = kCAFillModeForwards;
    [gradientShapeLayer addAnimation:gradientAnimation forKey:NSStringFromSelector(@selector(strokeEnd))];
    //[self gradientSpaceWithShapeLayer:gradientShapeLayer];
    
}

-(void)gradientSpaceWithShapeLayer:(CAShapeLayer*)gradientShapeLayer
{
    CAGradientLayer * gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.mask = gradientShapeLayer;
    NSArray * colors = @[
                         (__bridge id)[UIColor redColor].CGColor,
                         (__bridge id)[UIColor yellowColor].CGColor,
                         (__bridge id)[UIColor blueColor].CGColor,
                         (__bridge id)[UIColor greenColor].CGColor,
                         ];
    gradientLayer.colors     = colors;
    gradientLayer.locations  = @[@(0.0f),@(0.8f)];
    gradientLayer.startPoint = CGPointMake(0.3, 0.3);
    gradientLayer.endPoint   = CGPointMake(.8, .9);
    gradientLayer.type       = kCAGradientLayerAxial;
    [self.layer addSublayer:gradientLayer];
}

-(void)drawGradientLayerWithContextRef:(CGContextRef)contextRef
                                  path:(CGPathRef)mutablePathRef
                            startColor:(CGColorRef)startColor
                              endColor:(CGColorRef)endColor
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = {0.0 , .8f};
    NSArray * colors = @[
                         (__bridge id) startColor,
                         (__bridge id) endColor,
                         ];
    CGGradientRef gradientRef = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
    CGRect  pathRect    =  CGPathGetBoundingBox(mutablePathRef);
    CGPoint  startPoint =  CGPointMake(CGRectGetMinX(pathRect), CGRectGetMinY(pathRect));
    CGPoint  endpoint   =  CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMaxY(pathRect));
    CGContextAddPath(contextRef, mutablePathRef);
    CGContextClip(contextRef);
    CGContextDrawLinearGradient(contextRef, gradientRef, startPoint, endpoint, kCGGradientDrawsBeforeStartLocation);
}
@end

#import <CoreGraphics/CoreGraphics.h>
@interface TrackGradientView : UIView
#define kAngelTransArcProgress(angle) (angle *M_PI/180.f)

@property (strong, nonatomic) CAShapeLayer * trackShapeLayer;

@end

@implementation TrackGradientView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    CGRect rect = CGRectMake( 100,100,200, 200);
    UIView * bgView = [[ UIView alloc] initWithFrame:rect];
    bgView.backgroundColor = [UIColor clearColor];
    [ self addSubview:bgView];
    CAShapeLayer * trackLayer = [ CAShapeLayer layer];
    trackLayer. frame = bgView. bounds;
    [bgView.layer addSublayer:trackLayer];
    
    trackLayer.fillColor = [[ UIColor clearColor]CGColor];//填充颜色
    trackLayer.strokeColor = [[UIColor whiteColor]CGColor];//边框颜色
    trackLayer.lineCap = kCALineCapRound ;
    trackLayer.lineWidth = 10.0; // 红色的边框宽度
    UIBezierPath * path  = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:88 startAngle:0 endAngle:2*M_PI clockwise:YES];
    trackLayer.path =  path.CGPath;
    _trackShapeLayer  = trackLayer;
    //
    CALayer * gradientLayer = [CALayer layer];
    gradientLayer.frame  = bgView.bounds;
    CAGradientLayer * gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame  = CGRectMake(0,0, bgView.frame.size.width/2.f, bgView.frame.size.height/1.f);
    NSArray * colors = @[
                          ( id)[UIColor redColor].CGColor,
                          ( id)[UIColor yellowColor].CGColor,
                          ( id)[UIColor blueColor].CGColor
                         ];
    gradientLayer1.colors = colors;
    gradientLayer1.position = CGPointMake(50, 100);
    [gradientLayer1 setLocations:@[@(.4),@(.6)]];
    [gradientLayer1 setEndPoint:CGPointMake(.9, .9)];
    [gradientLayer addSublayer:gradientLayer1];
    //
     CAGradientLayer * gradientLayer2 = [CAGradientLayer layer];
     gradientLayer2.frame  =  CGRectMake(bgView.frame.size.width/2.f, 0, bgView.frame.size.width/2.f, bgView.frame.size.height);
    
    gradientLayer2.colors = colors;
    gradientLayer2.startPoint = CGPointMake(0,.5);
    gradientLayer2.endPoint  = CGPointMake(.5, 1.f);
    [gradientLayer addSublayer:gradientLayer2];
    
     gradientLayer.mask = trackLayer;
    [bgView.layer addSublayer:gradientLayer];

    [self  displayTrackShape];
}

- (void)displayTrackShape
{
    CAKeyframeAnimation * transformAnimation  = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    transformAnimation.path =  _trackShapeLayer.path;
    transformAnimation.cumulative =  YES;
    transformAnimation.repeatCount = MAXFLOAT;
    [_trackShapeLayer addAnimation:transformAnimation forKey:@"position"];
}
@end

#import "ColumeBarController.h"
@interface ColumeBarController()

@property (strong, nonatomic) ColumeDrawView * columnDrawView;
@property (strong, nonatomic) GradientSpaceView * gradientSpaceView;
@property (strong, nonatomic) TrackGradientView * trackGradientView;

@end
@implementation ColumeBarController
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor  = [UIColor whiteColor];
    //    [self drawColumnSubView];
//    [self drawGradientSpaceView];
    [self  drawTrackGradientView];
    [self initWithSubView];
}

#pragma mark ViewController AsynRequestData
#pragma mark ViewController Algorithm Processor
-(void)drawGradientSpaceView
{
    [self.view addSubview:self.gradientSpaceView];
}

-(void)drawColumnSubView
{
    [self.view addSubview:self.columnDrawView];
}
-(void)drawTrackGradientView
{
    [self.view addSubview:self.trackGradientView];
}

#pragma mark ViewController Initialize SubObjectView
- (void)initWithSubView
{
    [self customNavigationBarStyleWithleftBarItems:@[@"",@""]
                               navigationItemTitle:@"centerVC"
                            andRightBarButtonItems:@[@"",@""]
                         leftBarItemHandleComplete:^(NSInteger idx, UIButton *parameter)
     {
         
     }
                        rightBarItemHanbleComplete:^(NSInteger idx, UIButton *parameter)
     {
     }];
    [self setNavigationItemTitle:@"gradientSpace"];
}

#pragma mark --viewController getter methods
- (ColumeDrawView *)columnDrawView
{
    _columnDrawView = ({
        if (!_columnDrawView) {
            _columnDrawView  = [[ColumeDrawView alloc]initWithFrame:CGRectZero];
        }
        _columnDrawView;
    });
    return _columnDrawView;
}
-(GradientSpaceView *)gradientSpaceView
{
    _gradientSpaceView = ({
        if (!_gradientSpaceView) {
            _gradientSpaceView  = [[GradientSpaceView alloc]initWithFrame:CGRectZero];
        }
        _gradientSpaceView;
    });
    return _gradientSpaceView;
}
-(TrackGradientView *)trackGradientView
{
    _trackGradientView = ({
        if (!_trackGradientView) {
            _trackGradientView  = [[TrackGradientView alloc]initWithFrame:CGRectZero];
        }
        _trackGradientView;
    });
    return _trackGradientView;
}
#pragma mark Possess  Delegate ViewController（委托代理者）
#pragma mark IBOutlet Action ViewController

#pragma mark Other Method
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    //  self.columnDrawView.frame = CGRectMake(0, kNavigationBarHeight, kScreen_Width, kScreen_Height - kNavigationBarHeight - 44);
//    self.gradientSpaceView.frame  = CGRectMake(0, kNavigationBarHeight, kScreen_Width, kScreen_Height - kNavigationBarHeight - 44);
     self.trackGradientView.frame = CGRectMake(0, kNavigationBarHeight, kScreen_Width, kScreen_Height - kNavigationBarHeight - 44);
}

@end